#ifndef __MCCANVAS_H_
#define __MCCANVAS_H_

#include <qgraphicsview.h>
#include <ICanvas.h>
#include <vector>
#include <MCPanController.h>
#include <MCZoomController.h>
#include <MCCommandManager.h>
#include <MCSelectController.h>
#include <MCGripMover.h>
#include <MCConstraintsManager.h>

class MCCanvas : public QGraphicsView, public ICanvas
{
public:

    MCCanvas(QWidget *parent = nullptr);
   
    void addVisualItem(IGraphicsItem *item)  override;
    void addCursorVisual(ICursorVisual *cursorVisual)  override;
    void removeVisualItem(IGraphicsItem *item)  override;

    IGraphicsItem *itemAt(const MCPoint2D &position) override;
    IGripPoint *gripAt(const MCPoint2D &position)  override;
    std::vector<IGraphicsItem *> items()  override;
    void resetGrips()  override;

    int width() const override;
    int height() const override;
    MCPoint2D screenToWorld(const MCPoint2D &point)  override;
    MCPoint2D worldToScreen(const MCPoint2D &point)  override;
    double scale()  override;
    const MCVector2 &offset()  override;
    void setDrawingOffset(const MCVector2 &offset)  override;
    void setDrawingScale(double scale)  override;
    void redraw()  override;

    void keyPressEvent(QKeyEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;

    MCConstraintsManager *constraintManager() override;
    MCCommandManager *commandManager();
    MCSelectController *selectController();

private:

    void setupScene();
    void setupControllers();

private:

    QGraphicsScene *_scene;

    std::vector<IGraphicsItem *> _visualItems;
    std::vector<IGripPoint *> _gripPoints;

    double _sceneScale;
    MCVector2 _sceneOffset;

    // Controllers
    MCPanController *_panController;
    MCZoomController *_zoomController;
    MCCommandManager *_commandManager;
    MCSelectController *_selectController;
    MCGripMover *_gripController;
    MCConstraintsManager *_constraintManager;

};

#endif // !__MCCANVAS_H_
